<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="form"
      :rules="rules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <img src="https://likede2-admin.itheima.net/img/logo.595745bd.png" alt>
      </div>

      <el-form-item prop="loginName">
        <span class="svg-container el-icon-mobile-phone" />
        <el-input v-model="form.loginName" placeholder="请输入账号" />
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input ref="pawinp" v-model="form.password" placeholder="请输入密码" :type="inputType" />
        <span class="svg-container" @click="changpassword">
          <svg-icon :icon-class="inputType ==='password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>
      <el-form-item prop="Verification">
        <span class="svg-container">
          <svg-icon icon-class="yanzheng" />
        </span>
        <el-input v-model="form.code" placeholder="请输入验证码" />
        <div class="pic" @click="picimgfn">
          <img :src="picimg" alt>
        </div>
      </el-form-item>

      <el-button
        class="loginBtn"
        type="primary"
        :loading="loading"
        style="width:100%;margin-bottom:30px;"
        @click="submit"
      >Login</el-button>
    </el-form>
  </div>
</template>
<script>
// import { picimgApi } from '@/api/user'
export default {
  name: 'ViewsLogin',
  data() {
    return {
      inputType: 'password',
      form: {
        // clientToken: this.$store.state.user.clientToken,
        code: '',
        loginName: 'admin',
        loginType: 0,
        password: 'admin'
      },
      picimg: '',
      client: 0,
      loading: false,
      rules: {
        loginName: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z]\w{4,15}$/,
            message: '账号格式不正确',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 4,
            max: 16,
            message: '密码的长度在6-16位之间 ',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.getVerification()
  },
  methods: {
    changpassword() {
      this.inputType === 'password'
        ? (this.inputType = 'type')
        : (this.inputType = 'password')
      this.$nextTick(() => this.$refs.pawinp.focus())
    },
    async submit() {
      try {
        this.loading = true
        await this.$refs.loginForm.validate()
        await this.$store.dispatch('user/loginAction', {
          ...this.form,
          clientToken: this.$store.state.user.clientToken
        })
        this.loading = false
        this.$router.push('/')
      } catch (error) {
        console.log(error)
        this.loading = false
      }
    },
    async getVerification() {
      this.picimg = await this.$store.dispatch(
        'user/picimgAction',
        Math.round(Math.random() * 1000)
      )
    },
    picimgfn() {
      this.getVerification()
    }
  }
}
</script>

<style lang="scss">
$bg: #283443;
$light_gray: #68b0fe;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      // -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(255, 255, 255, 0.7);
    border-radius: 5px;
    color: #454545;
  }
  .el-form-item__erro {
    color: #fff;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #68b0fe;

.login-container {
  position: absolute;
  height: 100%;
  width: 100%;
  overflow: hidden;
  background-image: url('https://likede2-admin.itheima.net/img/background.be4fae7d.png');
  background-repeat: no-repeat;
  background-size: cover;

  .login-form {
    position: relative;
    width: 518px;
    height: 388px;
    top: 50%;
    left: 50%;
    margin-top: -194px;
    margin-left: -259px;
    padding: 76px 35px 0;
    background: #fff;
    -webkit-box-shadow: 0 3px 70px 0 rgb(30 111 72 / 35%);
    box-shadow: 0 3px 70px 0 rgb(30 111 72 / 35%);
    border-radius: 10px;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  ::v-deep .el-form-item__content {
    border: 1px solid #e2e2e2;
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: absolute;
    width: 96px;
    height: 96px;
    top: 0;
    left: 50%;
    transform: translate(-50%, -50%);
    img {
      width: 100%;
      height: 100%;
    }

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .loginBtn {
    background: #407ffe;
    height: 64px;
    line-height: 32px;
    font-size: 24px;
  }
}
.pic {
  width: 130px;
  height: 50px;
  background-color: #68b0fe;
  position: absolute;
  right: 0;
  top: 0;
}

</style>
